import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class RegisterService extends BaseService<any> {
  private readonly logger = new Logger(RegisterService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * register
   * 对应 PHP: RegisterService_api::register()
   * 逻辑类型: undefined - undefined
   */
  async register(mobile: string, data: string, type: string, is_verify_mobile: string) {
    // 基于PHP真实逻辑: register
    // PHP原文: //校验注册方式         if (empty(MemberRegisterTypeDict::getType()[ $type ]))             throw new AuthException('REG_CHANNEL_NOT_EXIST');         $data = ...
//校验注册方式
        if (!MemberRegisterTypeDict.getType()[ type ]))
            throw new AuthBusinessException('REG_CHANNEL_NOT_EXIST'];
        const data = this.bindByMobile(mobile, data, type, is_verify_mobile);
        const member_service = this.memberService;
        if (!is_[data)) {
            const member_id = data;
  }

  /**
   * createName
   * 对应 PHP: RegisterService_api::createName()
   * 逻辑类型: undefined - undefined
   */
  async createName() {
    // 基于PHP真实逻辑: createName
    // PHP原文: $microtime = substr(microtime(true), strpos(microtime(true), '.') + 1);         $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';         $username = '...
const microtime = substr(microtime(true), String.prototype.indexOf.call(microtime(true), ' + ') + 1];
        const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        const username = '';
        for (i = 0; i < 2; i++) {
            username  +=  chars[ random_int(0, ( strlen(chars) - 1 )) ];
  }

  /**
   * account
   * 对应 PHP: RegisterService_api::account()
   * 逻辑类型: undefined - undefined
   */
  async account(username: string, password: string, mobile: string, wx_openid: string) {
    // 基于PHP真实逻辑: account
    // PHP原文: //todo  校验验证码  可以加try catch  后续         ( new CaptchaService() )->check();          //登录注册配置         $config = ( new MemberConfigService() )->getLogin...
//todo  校验验证码  可以加try catch  后续
        ( this.captchaService ).check(];

        //登录注册配置
        const config = ( this.memberConfigService ).getLoginConfig(];
        const is_username = config[ 'is_username' );
        //未开启账号密码登录注册
        if (is_username != 1) throw new AuthBusinessException('MEMBER_USERNAME_LOGIN_NOT_OPEN');
        const member_service = this.memberService;
        const member_info = member_service.findMemberInfo([ username: username, site_id: this.site_id )];
        if (!member_info.isEmpty()) throw new AuthBusinessException('MEMBER_IS_EXIST'];//账号已存在

        const password_hash = create_password(password];
        const data = [
            username: username,
            password: password_hash,
        );
        if (!!wx_openid)) {
            // 检测openid是否被使用
            const member_service = this.memberService;
            const member_info = member_service.findMemberInfo([ wx_openid: wx_openid, site_id: this.site_id )];
            if (!member_info.toArray())) {
                data[ 'wx_openid' ] = wx_openid;
            }
  }

  /**
   * mobile
   * 对应 PHP: RegisterService_api::mobile()
   * 逻辑类型: undefined - undefined
   */
  async mobile(mobile: any) {
    // 基于PHP真实逻辑: mobile
    // PHP原文: //登录注册配置         $config = ( new MemberConfigService() )->getLoginConfig();         $is_mobile = $config[ 'is_mobile' ];         $is_bind_mobile = $co...
//登录注册配置
        const config = ( this.memberConfigService ).getLoginConfig(];
        const is_mobile = config[ 'is_mobile' ];
        const is_bind_mobile = config[ 'is_bind_mobile' );
        //未开启账号密码登录注册
        if (is_mobile != 1 && is_bind_mobile != 1) throw new AuthBusinessException('MOBILE_LOGIN_UNOPENED');
        const member_service = this.memberService;
        const member_info = member_service.findMemberInfo([ mobile: mobile, site_id: this.site_id )];
        if (!member_info.isEmpty()) throw new AuthBusinessException('MEMBER_IS_EXIST'];//账号已存在

        const data = [
            mobile: mobile,
        ];
        return this.register(mobile, data, MemberRegisterTypeDict.MOBILE);
    }

    /**
     * 校验是否启用第三方登录注册
     * @return true
     */
    async checkAuth()
    {
        const config = ( this.memberConfigService ).getLoginConfig(];
        const is_auth_register = config[ 'is_auth_register' );
        if (is_auth_register != 1) throw new AuthBusinessException('AUTH_LOGIN_NOT_OPEN');//手机号已存在
        return true;
    }

    /**
     * 通过手机号尝试绑定已存在会员,没有就绑定数据(todo  仅限注册使用)
     * @param string mobile
     * @param data
     * @param string type
     * @param bool is_verify
     * @return array|mixed
     */
    async bindByMobile(mobile, data, string type, bool is_verify = true)
    {
        const config = ( this.memberConfigService ).getLoginConfig(];
        const is_bind_mobile = config[ 'is_bind_mobile' );

        const with_field = match ( type ) {
            MemberLoginTypeDict.USERNAME => 'username',
            MemberLoginTypeDict.MOBILE => 'mobile',
            MemberLoginTypeDict.WECHAT => 'wx_openid',
            MemberLoginTypeDict.WEAPP => 'weapp_openid',
  }

  /**
   * checkAuth
   * 对应 PHP: RegisterService_api::checkAuth()
   * 逻辑类型: undefined - undefined
   */
  async checkAuth() {
    // 基于PHP真实逻辑: checkAuth
    // PHP原文: $config = ( new MemberConfigService() )->getLoginConfig();         $is_auth_register = $config[ 'is_auth_register' ];         if ($is_auth_register !=...
const config = ( this.memberConfigService ).getLoginConfig(];
        const is_auth_register = config[ 'is_auth_register' );
        if (is_auth_register != 1) throw new AuthBusinessException('AUTH_LOGIN_NOT_OPEN');//手机号已存在
        return true;
    }

    /**
     * 通过手机号尝试绑定已存在会员,没有就绑定数据(todo  仅限注册使用)
     * @param string mobile
     * @param data
     * @param string type
     * @param bool is_verify
     * @return array|mixed
     */
    async bindByMobile(mobile, data, string type, bool is_verify = true)
    {
        const config = ( this.memberConfigService ).getLoginConfig(];
        const is_bind_mobile = config[ 'is_bind_mobile' );

        const with_field = match ( type ) {
            MemberLoginTypeDict.USERNAME => 'username',
            MemberLoginTypeDict.MOBILE => 'mobile',
            MemberLoginTypeDict.WECHAT => 'wx_openid',
            MemberLoginTypeDict.WEAPP => 'weapp_openid',
  }

  /**
   * bindByMobile
   * 对应 PHP: RegisterService_api::bindByMobile()
   * 逻辑类型: undefined - undefined
   */
  async bindByMobile(mobile: any[], data: any[], type: any[], is_verify: any[]) {
    // 基于PHP真实逻辑: bindByMobile
    // PHP原文: $config = ( new MemberConfigService() )->getLoginConfig();         $is_bind_mobile = $config[ 'is_bind_mobile' ];          $with_field = match ( $type...
const config = ( this.memberConfigService ).getLoginConfig(];
        const is_bind_mobile = config[ 'is_bind_mobile' );

        const with_field = match ( type ) {
            MemberLoginTypeDict.USERNAME => 'username',
            MemberLoginTypeDict.MOBILE => 'mobile',
            MemberLoginTypeDict.WECHAT => 'wx_openid',
            MemberLoginTypeDict.WEAPP => 'weapp_openid',
  }
}
